package org.seeplnframework.skyeye.common.util;

import org.apache.commons.lang3.StringUtils;
import org.apache.skywalking.apm.agent.core.context.ContextManager;
import org.apache.skywalking.apm.agent.core.context.CorrelationContext;

public class SkyUtil {

    /**
     * 获取数据
     * @param key
     * @return
     */
    public static String get(String key) {
        String value = null;
        try {
            if (StringUtils.isNotBlank(key)) {
                CorrelationContext correlationContext = ContextManager.getCorrelationContext();
                if (correlationContext != null) {
                    value = correlationContext.get(key).orElse("");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return value != null ? value : "";
    }

    /**
     * put数据
     * @param key
     * @param value
     */
    public static void put(String key,String value) {
        try {
            if (StringUtils.isNotEmpty(key) && value != null) {
                CorrelationContext correlationContext = ContextManager.getCorrelationContext();
                if (correlationContext != null) {
                    correlationContext.put(key, value);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
